﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GameSound;


namespace MainGame.Menu
{

  public interface IMainMenu
  {

    event MenuEvent OnSettingsClicked;
    event MenuEvent OnHelpClicked;
    event MenuEvent OnCreditsClicked;
    event MenuEvent OnHighScoresClicked;

  }


  public class MainMenu : BaseMenu, IMainMenu
  {

    Button settingsBtn, creditsBtn, helpBtn, highScoresBtn;
    SpriteFont largeFont;
    public int colorBtnTimer;

    public MainMenu(Game game, MenuDepths depth)
      : base(game, depth)
    {
      background = game.Content.Load<Texture2D>(@"Images//Menu//startMenu");
      bounds = new Rectangle(100, 40, 235, 353);
      game.Services.AddService(typeof(IMainMenu), this);
    }

    protected override void Dispose(bool disposing) 
    {
      Game.Services.RemoveService(typeof(IMainMenu));
      base.Dispose(disposing);
    }


    public override void Initialize()
    {       

      //Font
      largeFont = Game.Content.Load<SpriteFont>("baveuse1_font");

      // Load all buttons
      int strLen = (int)largeFont.MeasureString(Locale.Strings.Settings).X;
      //settingsBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + ((bounds.Width - strLen) / 2), 110, 180, 50), Color.White, "baveuse1_font", Locale.Strings.Settings, Color.MediumBlue, 0.8f);
      settingsBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + 25, 110, 180, 50), Color.Transparent, "baveuse1_font", Locale.Strings.Settings, Color.MediumBlue, 0.8f);
      AddButton(settingsBtn, () =>
      {
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);
        settingsBtn.textColor = Color.Red;
        colorBtnTimer = 5;
       });

      strLen = (int)largeFont.MeasureString(Locale.Strings.Help).X;
      //helpBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + ((bounds.Width - strLen) / 2) - 42, 167, 180, 50), Color.White, "baveuse1_font", Locale.Strings.Help, Color.MediumBlue, 0.8f);
      helpBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + 25, 167, 180, 50), Color.Transparent, "baveuse1_font", Locale.Strings.Help, Color.MediumBlue, 0.8f);
      AddButton(helpBtn, () =>
      {
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);
        helpBtn.textColor = Color.Red;
        colorBtnTimer = 5;
      });

      strLen = (int)largeFont.MeasureString(Locale.Strings.HighScores.Replace(" ", "\n")).X;
      //highScoresBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + ((bounds.Width - strLen) / 2) + 12, 224, 180, 50), Color.White, "baveuse1_font", Locale.Strings.HighScores.Replace(" ", "\n"), Color.MediumBlue, 0.8f);
      highScoresBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + 25, 224, 180, 50), Color.Transparent, "baveuse1_font", Locale.Strings.HighScores.Replace(" ", "\n"), Color.MediumBlue, 0.8f);
      AddButton(highScoresBtn, () =>
      {
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);
        highScoresBtn.textColor = Color.Red;
        colorBtnTimer = 5;
      });


      strLen = (int)largeFont.MeasureString(Locale.Strings.Credits).X;
      //creditsBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + ((bounds.Width - strLen) / 2) - 8, 280, 180, 50), Color.White, "baveuse1_font", Locale.Strings.Credits, Color.MediumBlue, 0.8f);
      creditsBtn = new Button(Game, @"white_pixel", new Rectangle(bounds.X + 25, 280, 180, 50), Color.Transparent, "baveuse1_font", Locale.Strings.Credits, Color.MediumBlue, 0.8f);
      AddButton(creditsBtn, () =>
      {
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);
        creditsBtn.textColor = Color.Red;
        colorBtnTimer = 5;
      });

      base.Initialize();
    }

    public override void Update(GameTime gameTime)
    {

      if (settingsBtn.textColor == Color.Red)
      {
        colorBtnTimer--;
        if (colorBtnTimer < 0)
        {
          if (OnSettingsClicked != null)
            OnSettingsClicked();
        }
      }

      if (creditsBtn.textColor == Color.Red)
      {
        colorBtnTimer--;
        if (colorBtnTimer < 0)
        {
          if (OnCreditsClicked != null)
            OnCreditsClicked();
        }
      }

      if (helpBtn.textColor == Color.Red)
      {
        colorBtnTimer--;
        if (colorBtnTimer < 0)
        {
          if (OnHelpClicked != null)
            OnHelpClicked();
        }
      }

      if (highScoresBtn.textColor == Color.Red)
      {
        colorBtnTimer--;
        if (colorBtnTimer < 0)
        {
          if (OnHighScoresClicked != null)
            OnHighScoresClicked();
        }
      }
      
      base.Update(gameTime);
    }

    public event MenuEvent OnSettingsClicked;
    public event MenuEvent OnHelpClicked;
    public event MenuEvent OnCreditsClicked;
    public event MenuEvent OnHighScoresClicked;
  }
}
